package com.mrjy.queries.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.dto.QuestionDto;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.QuestionInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.queries.bo.IQuestionInfoBo;
import com.mrjy.queries.dto.MiniMyQAQDTO;

@Controller
@RequestMapping("/queries/questionInfo")
public class QuestionInfoController{
	@Resource
	private IQuestionInfoBo questionInfoBo;
	
	
	/**
	 * 查询答疑信息列表-前端web
	 * 答疑--查询最新和最热问题列表
	 * @param questionDto questionDto.stage(阶段) questionDto.filter （方式：new 最新；hot：最热）
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/getQuestionList")
	@ResponseBody
	public List<QuestionInfo> getQuestionList(QuestionDto questionDto,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.queryQuestionInfosByPage(userInfo,questionDto);
	}
	
	/**
	 * 根据ID查询
	 * @return
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(@RequestParam(required=true) int id,HttpSession session) {
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.queryQuestionById(id,userInfo);
	}

	/**
	 * 查询用户个人的问题列表----分页
	 * 个人中心--我的问答--我的问题
	 * @param session
	 * @param questionDto questionDto.page ,questionDto.rows
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/getMyQuestionList")
	@ResponseBody
	public Object getMyQuestionList(HttpSession session,QuestionDto questionDto){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.queryMyQuestionInfos(questionDto,userInfo);
	}
	
	/**
	 * TODO 没有进行事务处理
	 * 添加
	 * 答疑--提问
	 * @param questionInfo
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/issueQuestion")
	@ResponseBody
	public Object issueQuestion(QuestionInfo question,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.insert(question,userInfo);
	}
	
	/**
	 * 显示信息列表-后台
	 * @return
	 */
	@RequestMapping("/questionListBack")
	@ResponseBody
	public Object questionListBack(QuestionInfo question,HttpSession session) {
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.queryQuestionInfosBack(question,userInfo);
	}
	
	/**
	 * 禁用、起用
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(QuestionInfo question,HttpSession session) {
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.txUpdateDisableFlag(question,userInfo);
	}
	
	/**
	 * 添加
	 * @return
	 */
	@RequestMapping("/issueQuestionBack")
	@ResponseBody
	public Object issueQuestionBack(QuestionInfo question,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.insert(question,userInfo);
	}
	
	/**
	 * 获取问题阶段列表
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/getStageList")
	@ResponseBody
	public Object getStageList(HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return questionInfoBo.getStageList(userInfo);
	}
	
	
	/**
	 * 根据问题名称模糊分页搜索
	 * @param question
	 * @return
	 * @author Javin : isUer = true, isNew = true,
	 */
	@RequestMapping("/getPageQuestionByContent")
	@ResponseBody
	public Object getPageQuestionByContent(QuestionInfo question){
		return questionInfoBo.getPageQuestionByContent(question);
	}
	
	/**
	 * 修改
	 * @param userNews
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById(QuestionInfo question,HttpSession session) {
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		HashMap<String,Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		question.setUserId(userInfo.getId());
		return questionInfoBo.txUpdateById(question,map);
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteByIds(int[] ids) {		
		return questionInfoBo.deleteByIds(ids);
	}
	
	/**
	 * 通过questionId查询问题信息
	 * @param id
	 * @author csshui
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
    public Object selectById(QuestionInfo questionInfo) {
        return questionInfoBo.selectById(questionInfo);
    }
	/**
	 * 通过questionId查询对应的用户的信息
	 * @param id
	 * @author huangfei
	 * @return
	 */
	@RequestMapping("selectUserById")
	@ResponseBody
	public Object selectUserById(@RequestParam("id")int id) {		
		return questionInfoBo.selectUserById(id);
	}
	
	/**
	 * 根据阶段查询最新问题
	 * @param info.page, info.rows, info.typeId
	 * @return
	 */
	@RequestMapping("/queryPageNewestQuestion")
	@ResponseBody
	public Object queryPageNewestQuestion(QuestionInfo info){
		return questionInfoBo.queryPageNewestQuestion(info);
	}
	
	/**
	 * 提问插入问题
	 * @param info
	 * @param session
	 * @return
	 * @author Javin : isUse = true,
	 */
	@RequestMapping("/insertQuestion")
	@ResponseBody
    public Object insertQuestion(QuestionInfo info, HttpSession session) {
        UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
        Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", 1);
			map.put("err_msg", "没有用户数据");
			return map;
		}
		info.setUserId(userInfo.getId());
        return questionInfoBo.txInsertQuestion(info, map);
    }

	/**
	 * 通过userId 查询 我的问题
	 * @param info info.userId
	 * @param session
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/queryMyQAPageByUserId")
	@ResponseBody
    public Object queryMyQAPageByUserId(QuestionInfo info, HttpSession session) {
        return questionInfoBo.queryMyQAPageByUserId(info, session);
    }
	
	/**
	 * 浏览量倒序查询最热问题
	 * @param info
	 * @author Javin
	 */
	@RequestMapping("/queryPageByViewNum")
	@ResponseBody
	public Object queryPageByViewNum(QuestionInfo info){
		return questionInfoBo.queryPageByViewNum(info);
	}
	
	/**
	 * 更新问题的浏览量
	 * @param qid 问题id
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/addQuestionViewNum")
	@ResponseBody
	public Object addQuestionViewNum(Integer qid){
		return questionInfoBo.txAddQuestionViewNum(qid);
	}
	
	/**
	 * 根据话题ID查找用户是否已经提问过
	 * @param info
	 * @param session
	 * @return
	 */
	@RequestMapping("/queryExistQuestionByTopicId")
	@ResponseBody
	public Object queryExistQuestionByTopicId(QuestionInfo info,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户信息不存在");
			return map;
		}
		info.setUserId(userInfo.getId());
		return questionInfoBo.queryExistQuestionByTopicId(info,map);
	}
	/**
	 * 通过userId查询用户的提问信息
	 * @param userId 用户id
	 * @return
	 */
	@RequestMapping("/queryMyQuestionByUserId")
	@ResponseBody
	public Object queryMyQuestionByUserId(QuestionInfo info){
		return questionInfoBo.queryMyQuestionByUserId(info);
	}
	/**
	 * @author Javin
	 * @date 2017年9月18日
	 * @desc 分页查询问题列表 for admin
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectQuestionListPage")
	@ResponseBody
	public Object selectQuestionListPage(QuestionInfo info,HttpSession session){
		return questionInfoBo.selectQuestionListPage(info,session);
	}
	
	/**
	 * pc端通过问题话题typeId查询用户的相关信息
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryFamilyQuestionUserInfoForPC")
	@ResponseBody
	public Object queryFamilyQuestionUserInfoForPC(QuestionInfo info){
		return questionInfoBo.queryFamilyQuestionUserInfoForPC(info);
	}

    /**
     * @Author : lyon
     * @Description : pc
     * @param : No such property: code for class: Script1
     * @Date : 下午 16:57 2017/7/17 0017
     */
    @RequestMapping("/dispatcherQuestion")
    @ResponseBody
    public Object dispatcherQuestion(QuestionInfo info){
        return questionInfoBo.dispatcherQuestion(info);
    }
	/**
	 *hfei   phone端最新，查询最新的提问和回答
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryPageNewestQuestionComment")
	@ResponseBody
    public Object queryPageNewestQuestionComment(QuestionInfo info, HttpSession session) {
        return questionInfoBo.queryPageNewestQuestionComment(info, session);
    }

    /**
	 *hfei   phone端 查询用户的回答被点赞最多的排序
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryIntelligent")
	@ResponseBody
	public Object queryIntelligent(QuestionInfo info){
		return questionInfoBo.queryIntelligent(info);
	}
	
	/**
	 * 导出excel
	 * @param info
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(QuestionInfo info,HttpServletResponse response,HttpSession session){
		return questionInfoBo.exportExl(info,response,session);
	}

    /**
     * lyon 最新
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryDynamicPageByInfo")
    @ResponseBody
    public Object queryDynamicPageByInfo(QuestionInfo info, QuestionComment comment, HttpSession session) {
        return questionInfoBo.queryDynamicPageByInfo(info, comment, session);
    }

    /**
     * lyon 搜索提问
     *
     * @param info
     * @param session
     * @return
     */
    @RequestMapping("/getPageSearchQuestionByContent")
    @ResponseBody
    public Object getPageSearchQuestionByContent(QuestionInfo info, HttpSession session) {
        return questionInfoBo.getPageSearchQuestionByContent(info, session);
    }


    /**
     * lyon 个人主页动态
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryUserDynamicPageByInfo")
    @ResponseBody
    public Object queryUserDynamicPageByInfo(QuestionInfo info, QuestionComment comment, HttpSession session) {
        return questionInfoBo.queryUserDynamicPageByInfo(info, comment, session);
    }

    /**
     * 自定义排名
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryTopicPageByInfo")
    @ResponseBody
    public Object queryTopicPageByInfo(QuestionInfo info, HttpSession session) {
        return questionInfoBo.queryTopicPageByInfo(info, session);
    }

    /**
     * hfei   phone端 查询用户的回答被点赞最多的排序
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryTopicIntelligent")
    @ResponseBody
    public Object queryTopicIntelligent(QuestionInfo info) {
        return questionInfoBo.queryTopicIntelligent(info);
    }

    /**
     * 微信1.5.1
     * 视频关联问题
     *
     * @param info
     * @return
     */
    @RequestMapping("/relatedQuestions")
    @ResponseBody
    public Object relatedQuestions(QuestionInfo info) {
        return questionInfoBo.relatedQuestions(info);
    }
    
    /**
     * 小程序，查询我的回答
     *
     * @param info
     * @return
     */
    @RequestMapping("/miniMyQAQ")
    @ResponseBody
    public Object miniMyQAQ(MiniMyQAQDTO info) {
        return questionInfoBo.miniMyQAQ(info);
    }
    
    /**
     * 1.6.9.2 
     * 家园-社区热帖
     * @param comment
     * @param session
     * @return
     */
    @RequestMapping("/home/post/list")
    @ResponseBody
    public Object list(QuestionInfo info, HttpSession session) {
        return questionInfoBo.queryPostPage(info, session);
    }
    
    /**
     * 1.6.9.2
     * 置顶帖子/取消置顶
     * @param questionId
     * @param session
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Integer questionId,String isStick,HttpSession session) {
        return questionInfoBo.txUpdateStickStatus(questionId,isStick, session);
    }
    
    @RequestMapping("/latestByTypes")
    @ResponseBody
    public Object latestByTypes(Integer[] types){
    	return questionInfoBo.latestByTypes(types);
    }

}
